package com.hjw.junit;

import com.hjw.oop.Student;

import java.util.Arrays;
import java.util.Scanner;
import java.util.UUID;

/**
 * 本类主要用于测试Student类
 * @author hjwStrart
 * @create 2022-01-07 14:59
 */
public class TestStudent {
    public static void main(String[] args) {
        Student[] stus = new Student[5];
        for (int i = 0; i < stus.length; i++) {
            stus[i] = new Student();
            stus[i].setName((i + 1) + "号学生") ;
            stus[i].setAge((int)(Math.random() * 100) );
            stus[i].setMajor("计科");
            stus[i].setNo(UUID.randomUUID().toString().substring(0,11));
        }
        int i = 0;
        boolean flag = false;
        Scanner scanner = new Scanner(System.in);
        while (true){
            try {
                i = Integer.parseInt(menu());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            switch (i){
                case 1:
                    getAllStusInfo(stus);break;
                case 2:
                {
                    System.out.print("请输入你要修改的学生姓名：");
                    String name = scanner.next();
                    updateStuInfoByName(stus,name);
                    break;
                }
                case 3:;break;
                case 4: flag = true;break;
                default:
                    System.out.println("请输入正确的选项！！！");break;
            }
            if (flag){
                break;
            }
        }
        System.out.println(Arrays.toString(stus));
    }

    public static String menu(){
        System.out.println("1.查看学生信息  2.修改学生信息  3.删除学生信息  4.退出");
        Scanner scanner = new Scanner(System.in);
        String accept = scanner.next();
        return accept;
    }

    /**
     * 输出所有的学生信息
     * @param stus
     */
    public static void getAllStusInfo(Student[] stus){
        for (Student student : stus) {
            System.out.println(student);
        }

    }

    /**
     * 返回指定学生的信息，通过姓名查找
     * @param stus
     * @param name 学生姓名
     * @return  查找返回索引，没找到返回 -1
     */
    public static int getStuInfo(Student[] stus,String name){

        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getName().equals(name)){
                return i;
            }
        }
        return -1;
    }


    /**
     * 通过学生姓名修改学生信息
     * @param stus  学生集合
     * @param name 学生姓名
     */
    public static void updateStuInfoByName(Student[] stus,String name){

        Scanner scanner = new Scanner(System.in);
        int index = getStuInfo(stus, name);
        if(index == -1) {
            System.out.println("不存在名为" + name + "的学生！");
        }else{
            System.out.println(name + "的信息如下：");
            System.out.println(stus[index]);
            System.out.println("请输入修改后的学生信息：");
            System.out.print("学生姓名：");
            String newName = scanner.next();
            System.out.print("学生学号：");
            String newNo = scanner.next();
            System.out.print("学生专业：");
            String newMajor = scanner.next();
            System.out.print("学生年龄：");
            int newAge = scanner.nextInt();
            stus[index] = new Student(newAge,newName,newNo,newMajor);
            System.out.println(name + "的信息修改完毕，修改后的信息如下");
            System.out.println(stus);
        }

    }
}
